package cn.home.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 邹洪辉
 * @version 1.0
 * @date 2023/3/2 - 19:30
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Users implements UserDetails {
    private Integer userId;
    private String userName;
    private String userPwd;
    private String userSex;
    private String userAge;
    private String role;
    private Integer deleted;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> roleList = new ArrayList<>();
        //分割角色字段
        String[] roles = role.split(",");
        for (String s : roles) {
            //添加到集合中
            roleList.add(new SimpleGrantedAuthority(s));
        }
        return roleList;
    }

    @Override
    public String getPassword() {
        return this.userPwd;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
